package com.zhizuobiao.class2;

import com.zhizuobiao.dao.UserDao;
import com.zhizuobiao.entity.QueryTest;
import com.zhizuobiao.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.Map;

/**
 * 不推荐使用自己实现接口的方式
 * ##使用动态代理方式##
 */
public class UserDaoImpl implements UserDao {

    private SqlSessionFactory sqlSessionFactory;


    //注入SqlSessionFactory
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }


    public User getUser(int id) throws Exception {
        SqlSession session = sqlSessionFactory.openSession();
        User user = null;
        try {
            //通过sqlsession调用selectOne方法获取一条结果集
            //参数1：指定定义的statement的id,参数2：指定向statement中传递的参数
            user = session.selectOne("test.getUser", id);
            System.out.println(user);

        } finally {
            session.close();
        }
        return user;
    }

    public List<User> getUserLike(String username) throws Exception {
        return null;
    }

    public List<User> getUserList2(User user) throws Exception {
        return null;
    }

    public List<User> getUserList3(List list) throws Exception {
        return null;
    }

    public List<User> getUserList(QueryTest query) throws Exception {
        return null;
    }

    public List<User> getUserListOrder(Map order) throws Exception {
        return null;
    }


    public int insertUser(User user) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int code = 0;
        try {
            code = sqlSession.insert("insertUser", user);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }

        return code;
    }

    public List<User> getUserListOrders() {
        return null;
    }

    public int updateUser(User user) {
        return 0;
    }
}
